<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料" prop="itemId">

        <el-select v-model="queryParams.itemId" placeholder="请选择物料" clearable>
          <el-option
            v-for="item in itemPullData"
            :key="item.id"
            :label="item.itemName"
            :value="item.id"
          />
        </el-select>

      </el-form-item>

      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库" clearable @change="handleWarehouseChange">
          <el-option
            v-for="warehouse in warehousePullData"
            :key="warehouse.id"
            :label="warehouse.warehouseName"
            :value="warehouse.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属库区" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择库区" clearable @change="handleAreaChange">
          <el-option
            v-for="area in filteredAreaData"
            :key="area.id"
            :label="area.areaName"
            :value="area.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="货架" prop="rackId">
        <el-select v-model="queryParams.rackId" placeholder="请选择货架" clearable>
          <el-option
            v-for="rack in filteredRackData"
            :key="rack.id"
            :label="rack.rackName"
            :value="rack.id"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['inventory:inventory:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['inventory:inventory:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['inventory:inventory:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--		  :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['inventory:inventory:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属仓库" align="center" prop="warehouseId" >
        <template slot-scope="scope">
          <warehouse-tag :options="warehousePullData" :value="scope.row.warehouseId"/>
        </template>

      </el-table-column>
      <el-table-column label="所属库区" align="center" prop="areaId"  >

        <template slot-scope="scope">
          <area-tag :options="areaPullData" :value="scope.row.areaId"/>
        </template>


      </el-table-column>/
      <el-table-column label="货架" align="center" prop="rackId"  >


        <template slot-scope="scope">
          <rack-tag :options="rackPullData" :value="scope.row.rackId"/>
        </template>

      </el-table-column>
      <el-table-column label="物料" align="center" prop="itemId" >

        <template slot-scope="scope">
          <item-tag :options="itemPullData" :value="scope.row.itemId"/>
        </template>

      </el-table-column>

      <el-table-column label="库存" align="center" prop="quantity" />
      <el-table-column label="备注" align="center" prop="remark"/>

<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--&lt;!&ndash;          <el-button&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            type="text"&ndash;&gt;-->
<!--&lt;!&ndash;            icon="el-icon-edit"&ndash;&gt;-->
<!--&lt;!&ndash;            @click="handleUpdate(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;            v-hasPermi="['inventory:inventory:edit']"&ndash;&gt;-->
<!--&lt;!&ndash;          >查看</el-button>&ndash;&gt;-->
<!--&lt;!&ndash;          <el-button&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            type="text"&ndash;&gt;-->
<!--&lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
<!--&lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;            v-hasPermi="['inventory:inventory:remove']"&ndash;&gt;-->
<!--&lt;!&ndash;          >删除</el-button>&ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属仓库" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入所属仓库" />
        </el-form-item>
        <el-form-item label="所属库区" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属库区" />
        </el-form-item>
        <el-form-item label="货架" prop="rackId">
          <el-input v-model="form.rackId" placeholder="请输入货架" />
        </el-form-item>
        <el-form-item label="物料" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入物料" />
        </el-form-item>

        <el-form-item label="库存" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory, exportInventory } from "@/api/inventory/inventory";
import { pullWarehouse } from '@/api/wms/warehouse'
import { pullArea } from '@/api/wms/area'
import { pullRack } from '@/api/wms/rack'
import { pullItem } from '@/api/wms/item'

export default {
  name: "Inventory",
  data() {
    return {
      filteredAreaData: [],   // 过滤后的库区数据
      filteredRackData: [] ,   // 过滤后的货架数据
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 所属库区字典
      idOptions: [],
      // 物料字典
      itemIdOptions: [],
      // 货架字典
      rackIdOptions: [],
      // 库存字典
      quantityOptions: [],
      // 备注字典
      remarkOptions: [],
      // 删除标识字典
      delFlagOptions: [],
      // 创建人字典
      createByOptions: [],
      // 创建时间字典
      createTimeOptions: [],
      // 修改人字典
      updateByOptions: [],
      // 修改时间字典
      updateTimeOptions: [],
      // 所属仓库字典
      warehouseIdOptions: [],
      // 所属库区字典
      areaIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: null,
        rackId: null,
        warehouseId: null,
        areaId: null
      },
      // 表单参数
      form: {},
      warehousePullData: [],
      areaPullData: [],
      rackPullData: [],
      itemPullData:[],
      // 表单校验
      rules: {
        itemId: [
          { required: true, message: "物料不能为空", trigger: "blur" }
        ],
        rackId: [
          { required: true, message: "货架不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        warehouseId: [
          { required: true, message: "所属仓库不能为空", trigger: "blur" }
        ],
        areaId: [
          { required: true, message: "所属库区不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

    /** 查询仓库下拉数据 **/
    pullWarehouse().then(response => {
      this.warehousePullData = response.data;
    });

    /** 查询库区下拉数据 **/
    pullArea().then(response => {
      this.areaPullData = response.data;
    });

    /** 查询库区下拉数据 **/
    pullRack().then(response => {
      this.rackPullData = response.data;
    });
    /** 查询库区下拉数据 **/
    pullItem().then(response => {
      this.itemPullData = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.idOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.itemIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.rackIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.quantityOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.remarkOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.delFlagOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.createByOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.createTimeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.updateByOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.updateTimeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.warehouseIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.areaIdOptions = response.data;
    });
  },
  methods: {

    handleWarehouseChange(warehouseId) {
      // 仓库改变时，重置库区和货架选择
      this.queryParams.areaId = null;
      this.queryParams.rackId = null;

      // 根据仓库ID过滤库区数据
      this.filteredAreaData = this.areaPullData.filter(area => area.warehouseId === warehouseId);

      // 清空货架数据
      this.filteredRackData = [];
    },
    handleAreaChange(areaId) {
      // 库区改变时，重置货架选择
      this.queryParams.rackId = null;

      // 根据库区ID过滤货架数据
      this.filteredRackData = this.rackPullData.filter(rack => rack.areaId === areaId);

  },


    /** 查询库存列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 所属库区字典翻译
    idFormat(row, column) {
      return this.selectDictLabel(this.idOptions, row.id);
    },
    // 物料字典翻译
    itemIdFormat(row, column) {
      return this.selectDictLabel(this.itemIdOptions, row.itemId);
    },
    // 货架字典翻译
    rackIdFormat(row, column) {
      return this.selectDictLabel(this.rackIdOptions, row.rackId);
    },
    // 库存字典翻译
    quantityFormat(row, column) {
      return this.selectDictLabel(this.quantityOptions, row.quantity);
    },
    // 备注字典翻译
    remarkFormat(row, column) {
      return this.selectDictLabel(this.remarkOptions, row.remark);
    },
    // 删除标识字典翻译
    delFlagFormat(row, column) {
      return this.selectDictLabel(this.delFlagOptions, row.delFlag);
    },
    // 创建人字典翻译
    createByFormat(row, column) {
      return this.selectDictLabel(this.createByOptions, row.createBy);
    },
    // 创建时间字典翻译
    createTimeFormat(row, column) {
      return this.selectDictLabel(this.createTimeOptions, row.createTime);
    },
    // 修改人字典翻译
    updateByFormat(row, column) {
      return this.selectDictLabel(this.updateByOptions, row.updateBy);
    },
    // 修改时间字典翻译
    updateTimeFormat(row, column) {
      return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
    },
    // 所属仓库字典翻译
    warehouseIdFormat(row, column) {
      return this.selectDictLabel(this.warehouseIdOptions, row.warehouseId);
    },
    // 所属库区字典翻译
    areaIdFormat(row, column) {
      return this.selectDictLabel(this.areaIdOptions, row.areaId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemId: null,
        rackId: null,
        quantity: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        warehouseId: null,
        areaId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInventory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除库存编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInventory(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有库存数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInventory(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
